<?php
class CoursesController extends AppController{

	var $uses = array('Course');

	function beforeFilter(){
		parent::beforeFilter();
	}

	/*
	 * standard method
	 */
	public function index(){
		$params = array();
		if(!empty($this->params['form'])
		&& (strlen($this->params['form']['find_course_name'])>0
		|| strlen($this->params['form']['find_professor_name'])>0
		)
		){
			$params = array(
				'conditions' => array(
					'course_name LIKE' => '%'.$this->params['form']['find_course_name'].'%',
					'concat(first_name," ",family_name) LIKE' => '%'.$this->params['form']['find_professor_name'].'%',
			)
			);
		}else{
			$this->params['form']['find_course_name']='';
			$this->params['form']['find_professor_name']='';
		}

		$this->data = $this->Course->find('all',$params);
		if(empty($this->data)){
			parent::doFlash(array('Info'=>'Es sind keine Kurse vorhanden'));
		}
	}

	function add(){

		if(!empty($this->data)){
			$ok = $this->Course->save($this->data);
			$name = $this->data['Course']['course_name'];
			if($ok){
				$message = sprintf($this->translate('courses/saved'),$name);
				parent::doFlash(array('Info' => $message));
				$this->redirect(array('controller' => 'courses', 'action' => 'index'));
			}else{
				$message = sprintf($this->translate('courses/notSaved'),$name);
				parent::doFlash(array('Fehler' => $message));
			}

		}

		$this->_addLists();
	}

	function _addLists(){
		// get professor  ---------------------
		$params = array(
			'fields' => array('Professor.id','Professor.select_identifier')
		);
		$profs = $this->Course->Professor->find('list',$params);
		$this->set('professors',$profs);

		// get CourseSpecialisation  ---------------------
		$params = array(
			'fields' => array('CourseSpecialisation.id','CourseSpecialisation.specialisation_name')
		);
		$spec = $this->Course->CourseSpecialisation->find('list',$params);
		$this->set('courseSpecialisations',$spec);

		// get profiles  ---------------------
		$params = array(
			'fields' => array('Profile.id','Profile.profile_name')
		);
		$profiles = $this->Course->Profile->find('list',$params);
		$this->set('profiles',$profiles);

		// get study braches  ---------------------
		$params = array(
			'fields' => array('StudyBranch.id','StudyBranch.branch_name')
		);
		$select = $this->Course->StudyBranch->find('list',$params);
		$this->set('studyBranches',$select);
	}

	function update($id=null){
		if(!empty($this->data)){
			if(is_numeric($id)){
				$this->_addLists();
				$this->Course->id = $id;
				$ok = $this->Course->save($this->data);
				$name = $this->data['Course']['course_name'];
				if($ok){
					parent::doFlash(array('Info'=>'Der Kurs '.$name.' wurde gespeichert.'));
					$this->redirect(array('action'=>'index'));
				}else{
					$message = 'Der Kurs '.$name.' konnte nicht gespeichert werden. '.$this->Session->error();
					parent::doFlash(array('Info'=>$message));
				}
			}else{
				parent::doFlash(array('Fehler'=>'Die übergebene ID ist keine gültige ID'));
			}
		}
		if(is_numeric($id)){
			$this->data = $this->Course->findById($id);
			$this->_addLists();
		}
	}

	/**
	 *	Delete a course 
	 * @param int $id - id for course to delete
	 */
	function delete($id){
		if(is_numeric($id)){
			$result = $this->Course->read('course_name',$id);
			$name = '"'.$result['Course']['course_name'].'"';
			$ok = $this->Course->delete($id);
			if($ok){
				parent::doFlash(array('Info'=>'Der Kurs '.$name.' wurde gelöscht'));
			}else{
				$message = 'Der Kurs '.$name.' konnte nicht gelöscht werden. '.$this->Session->error();
				parent::doFlash(array('Info'=>$message));
			}
		}else{
			parent::doFlash(array('Info'=>'Diese Funktion ist noch nicht implementiert.'));
		}
		$this->redirect(array('action'=>'index'));
	}

}
?>
